// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Joint.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Microsoft.Robotics.Kinematics
{
    using System.Runtime.Serialization;

    using Microsoft.Robotics.Numerics;

    /// <summary>
    /// Class to represent a joint
    /// </summary>
    [DataContract]
    public class Joint
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Joint" /> class.
        /// </summary>
        /// <param name="dhParam">the DH-parameter which specifies the transformation
        /// between the previous joint/base to the current joint, the rotation of the
        /// current joint is also considered in the calculation</param>
        /// <param name="offset">the offset value of this joint</param>
        /// <param name="min">lower bound for this joint.  For revolute joints, this is in radians;
        /// for prismatic joint, units can vary as long as it is consistent throughout the description</param>
        /// <param name="max">upper bound for this joint.  For revolute joints, this is in radians;
        /// for prismatic joint, units can vary as long as it is consistent throughout the description</param>
        public Joint(DHParameter dhParam, double offset, double min, double max)
        {
            this.DHParam = dhParam;
            this.Offset = offset;
            this.MinValue = min;
            this.MaxValue = max;
        }

        /// <summary>
        /// Gets or sets the value of this joint
        /// </summary>
        [DataMember]
        public double Value { get; set; }

        /// <summary>
        /// Gets the lower bound of this joint.
        /// For revolute joints, this is in radians;
        /// for prismatic joints, units can vary as long as it is
        /// consistent throughout the description
        /// </summary>
        [DataMember]
        public double MinValue { get; private set; }

        /// <summary>
        /// Gets the upper bound of this joint, in radians
        /// For revolute joints, this is in radians;
        /// for prismatic joints, units can vary as long as it is
        /// consistent throughout the description
        /// </summary>
        [DataMember]
        public double MaxValue { get; private set; }

        /// <summary>
        /// Gets or sets the DH-parameter of this joint
        /// </summary>
        [DataMember]
        private DHParameter DHParam { get; set; }

        /// <summary>
        /// Gets or sets the offset of this joint
        /// </summary>
        [DataMember]
        private double Offset { get; set; }

        /// <summary>
        /// Gets the type of this joint
        /// </summary>
        /// <returns>The type of the joint</returns>
        public JointType Type()
        {
            return this.DHParam.Type;
        }

        /// <summary>
        /// Gets the transformation matrix for this joint given the joint type and the joint angle.
        /// This transformation matrix specifies the transformation from the last joint to the current joint
        /// after the current joint has moved to the specific joint angle.
        /// </summary>
        /// <returns>The transformation matrix</returns>
        public Matrix4 GetOffsetTransform()
        {
            return this.GetOffsetTransform(this.Value);
        }

        /// <summary>
        /// Gets the transformation matrix for this joint given the joint type and the joint angle.
        /// This transformation matrix specifies the transformation from the last joint to the current joint
        /// after the current joint has moved to the specific joint angle.
        /// </summary>
        /// <param name="value">Joint angle</param>
        /// <returns>The transformation matrix</returns>
        public Matrix4 GetOffsetTransform(double value)
        {
            return this.DHParam.GetTransformMatrix(value + this.Offset);
        }
    }
}
